<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入用户姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入用户联系电话" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="消息类型" prop="msgType">
        <el-select v-model="queryParams.msgType" placeholder="请选择消息类型" clearable>
          <el-option v-for="dict in msg_dialog" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dialogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="用户姓名" align="center" prop="name" />
      <el-table-column label="用户联系电话" align="center" prop="phone" />
      <el-table-column label="留言主题" align="center" prop="msg" />
      <el-table-column label="消息类型" align="center" prop="msgType">
        <template #default="scope">
          <dict-tag :options="msg_dialog" :value="scope.row.msgType" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['solution:dialog:edit']">查看对话</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['solution:dialog:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改对话框管理对话框 -->
    <el-dialog center title="对话框" v-model="open" width="1200px" append-to-body class="dialog">
      <view class="message">
        <scrolldialog :dialogId="dialogId" :open="open"></scrolldialog>
      </view>
      <view class="textarea">
        <el-input class="input" type="textarea" v-model.trim="msg" @keydown.enter="sendMsg"></el-input>
        <view class="send" @click="sendMsg">
          发送
        </view>
      </view>
    </el-dialog>
  </div>
</template>

<script setup name="Dialog">
import { listDialog, delDialog, addDialog, updateDialog } from "@/api/solution/dialog";
import { addChat } from "../../../api/solution/chat";
import { ref } from "vue";
import scrolldialog from "./scrolldialog.vue";
const { proxy } = getCurrentInstance();
const { msg_dialog } = proxy.useDict('msg_dialog');

const dialogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const msg = ref('');
const dialogId = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    phone: null,
    msgType: null,
  },
  rules: {
    name: [
      { required: true, message: "用户姓名不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "用户联系电话不能为空", trigger: "blur" }
    ],
    msg: [
      { required: true, message: "留言主题不能为空", trigger: "blur" }
    ],
    solutionId: [
      { required: true, message: "方案留言对应id不能为空", trigger: "blur" }
    ],
    msgType: [
      { required: true, message: "消息类型不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询对话框管理列表 */
function getList() {
  loading.value = true;
  listDialog(queryParams.value).then(response => {
    dialogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

async function sendMsg() {
  console.log(msg.value);
  const res = await addChat({
    dialogueId: dialogId.value,
    dialogueContent: msg.value
  });
  msg.value = '';
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  dialogId.value = row.id;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除对话框管理编号为"' + _ids + '"的数据项？').then(function () {
    return delDialog(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

getList();
</script>

<style lang="scss" scoped>
.dialog {
  .message {
    display: block;
    width: 100%;
    height: 400px;
    background-color: red;
  }

  .textarea {
    display: block;
    width: 100%;
    height: 200px;
    // background-color: blue;
    position: relative;
    border: 1px solid #ccc;

    .input {
      :deep(.el-input__wrapper) {
        box-shadow: none !important;
      }

      :deep(.el-textarea__inner) {
        resize: none;
        height: 160px;
        box-shadow: none !important;
      }
    }

    .send {
      position: absolute;
      display: block;
      font-size: 20px;
      height: 40px;
      width: 100px;
      line-height: 40px;
      text-align: center;
      right: 30px;
      bottom: 20px;
      background-color: #e9e9e9;
      color: #07c160;
      border-radius: 5px;
    }
  }
}
</style>